package org.tony.common.lang;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class StringUtils
{

  public static final String encode(String str, String charset) throws UnsupportedEncodingException
  {
    return URLEncoder.encode(str, charset);
  }

  public static final String decode(String str, String charset) throws UnsupportedEncodingException
  {
    return URLDecoder.decode(str, charset);
  }

  /**
   * Splitting the string to string array with the specified regular expression  
   * @param str
   * @param regex
   * @return
   */
  public static final String[] split(String str, String regex)
  {
    Pattern pat = Pattern.compile(regex);
    Matcher mc = pat.matcher(str);
    if (mc.find() && mc.groupCount() > 0)
    {
      String[] result = new String[mc.groupCount()];
      for (int i = 1; i <= mc.groupCount(); i++)
      {
        result[i - 1] = mc.group(i);
      }
      return result;
    }
    else
    {
      return pat.split(str);
    }
  }

  public static final String replace(String str, String regex, String repalcement, int count)
  {
    Pattern pat = Pattern.compile(regex);
    Matcher mc = pat.matcher(regex);
    if (mc.find())
    {
      for (int i = 1; i <= count; i++)
      {
      }
    }
    return null;
  }

  public static final String replaceFirst(String regex, String replacement)
  {
    return null;
  }

  public static final String replaceLast(String regex, String replacement)
  {
    return null;
  }

  public static final String replaceAll(String regex, String replacement)
  {
    return null;
  }

}
